package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.ProdFeeInfoMapper;
import cc.rengu.igas.bomp.common.entity.ProdFeeInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/2
 */
public class ProdFeeInfoMapperImpl implements ProdFeeInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertProdFeeInfo(ProdFeeInfo prodFeeInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertProdFeeInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodFeeInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记产品费率信息失败,instId:<{}>,prodType:<{}>,baseProdCode:<{}>,iReturnCode:<{}>", prodFeeInfo.getInstId(), prodFeeInfo.getProdType(), prodFeeInfo.getBaseProdCode(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateProdFeeInfoById(ProdFeeInfo prodFeeInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateProdFeeInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodFeeInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记产品费率信息失败,instId:<{}>,prodType:<{}>,baseProdCode:<{}>,iReturnCode:<{}>", prodFeeInfo.getInstId(), prodFeeInfo.getProdType(), prodFeeInfo.getBaseProdCode(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public ProdFeeInfo selectProdFeeInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdFeeInfoById");
        ProdFeeInfo prodFeeInfo = new ProdFeeInfo();
        prodFeeInfo.setInstId(instId);
        prodFeeInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodFeeInfo, prodFeeInfo);
        if (0 != iReturnCode) {
            rglog.error("获取产品费率信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return prodFeeInfo;
    }

    @Override
    public ProdFeeInfo selectProdFeeInfoByPrimaryKey(String instId, String prodType, String baseProdCode, String feeType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdFeeInfoByPrimaryKey");
        ProdFeeInfo prodFeeInfo = new ProdFeeInfo();
        prodFeeInfo.setInstId(instId);
        prodFeeInfo.setProdType(prodType);
        prodFeeInfo.setBaseProdCode(baseProdCode);
        prodFeeInfo.setFeeType(feeType);
        prodFeeInfo.setFeeEffectiveDate(DateUtil.getCurrentDate());
        prodFeeInfo.setFeeExpiryDate(DateUtil.getCurrentDate());
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodFeeInfo, prodFeeInfo);
        if (0 != iReturnCode) {
            rglog.error("获取产品费率信息失败,instId:<{}>,prodType:<{}>,baseProdCode:<{}>,feeType:<{}>,iReturnCode:<{}>", instId, prodType, baseProdCode, feeType, iReturnCode);
            return null;
        }
        return prodFeeInfo;
    }

    @Override
    public List<ProdFeeInfo> selectProdFeeInfoByBaseProdCode(String instId, String prodType, String baseProdCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdFeeInfoByBaseProdCode");
        ProdFeeInfo prodFeeInfo = new ProdFeeInfo();
        prodFeeInfo.setInstId(instId);
        prodFeeInfo.setProdType(prodType);
        prodFeeInfo.setBaseProdCode(baseProdCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, prodFeeInfo, ProdFeeInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取产品费率信息失败,instId:<{}>,prodType:<{}>,baseProdCode:<{}>", instId, prodType, baseProdCode);
            return null;
        }
        return objectList.stream().map(item -> (ProdFeeInfo) item).collect(Collectors.toList());
    }


    @Override
    public List<ProdFeeInfo> selectProdFeeInfoByBaseProdCodeAndProdCode(String instId, String baseProdCode, String prodCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdFeeInfoByBaseProdCodeAndProdCode");
        ProdFeeInfo prodFeeInfo = new ProdFeeInfo();
        prodFeeInfo.setInstId(instId);
        prodFeeInfo.setProdCode(prodCode);
        prodFeeInfo.setBaseProdCode(baseProdCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, prodFeeInfo, ProdFeeInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取产品费率信息失败,instId:<{}>,baseProdCode:<{}>,prodCode:<{}>,", instId, baseProdCode, prodCode);
            return null;
        }
        return objectList.stream().map(item -> (ProdFeeInfo) item).collect(Collectors.toList());
    }
}
